package com.ui.componet {
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import starling.display.Button;
	import starling.display.DisplayObject;
	import starling.textures.Texture;
	
	/**
	 * 圆角按钮 2015/3/6 16:25
	 */
	public class RoundButton extends Button {
		
		public var id:String = "";
		
		public function RoundButton(upState:Texture, text:String = "", downState:Texture = null) {
			super(upState, text, downState);
		}
		
		public override function hitTest(localPoint:Point, forTouch:Boolean = false):DisplayObject {
			// When the user touches the screen, this method is used to find out if an object was 
			// hit. By default, this method uses the bounding box, but by overriding it, 
			// we can change the box (rectangle) to a circle (or whatever necessary).
			
			// when the hit test is done to check if a touch is hitting the object, invisible or
			// untouchable objects must cause the hit test to fail.
			if (forTouch && (!visible || !touchable))
				return null;
			
			// get center of button
			var bounds:Rectangle = this.bounds;
			var centerX:Number = bounds.width / 2;
			var centerY:Number = bounds.height / 2;
			
			// calculate distance of localPoint to center. 
			// we keep it squared, since we want to avoid the 'sqrt()'-call.
			var sqDist:Number = Math.pow(localPoint.x - centerX, 2) + Math.pow(localPoint.y - centerY, 2);
			
			// when the squared distance is smaller than the squared radius, 
			// the point is inside the circle
			var radius:Number = bounds.width / 2 - 8;
			if (sqDist < Math.pow(radius, 2))
				return this;
			else
				return null;
		}
	}
}